home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CDTV / cdtvtools-11 / CD-DA / Lattice / example.lat.c next >
Encoding:
C/C++ Source or Header  |  1991-06-25  |  10.4 KB  |  487 lines

  1. /***********************************************************************
  2. ***
  3. ***     CDTV Audio Examples by Carl Sassenrath (10-Dec-90)
  4. ***
  5. ***     Copyright (C) 1990 Commodore International, Ltd.
  6. ***     Permission granted to use for your CDTV programs.
  7. ***
  8. ************************************************************************
  9. ***
  10. ***     This file contains examples of how to perform a few simple
  11. ***     CD Audio commands on CDTV.  It was cranked out in a few
  12. ***     hours to help illustrate a few effects... and it all seems
  13. ***     to work (could use a little tweaking maybe). Sorry, but I have
  14. ***     not had the chance to convert it from Manx C to Lattice C, so
  15. ***     if you do, please send me a copy.  If not, I will get too it
  16. ***     RSN...  -Carl-
  17. ***
  18. ***     Send comments, problems, etc. to:
  19. ***         Sassenrath, P.O. Box 1510, Ukiah, CA 95482
  20. ***         Phone: (707) 462-4878  FAX: (707) 462-4879
  21. ***         CBM VAX: carl
  22. ***
  23. ***********************************************************************
  24. ***
  25. ***    Modified for Lattice-C(ver5.05)    by Kaori Kuwata (4-Jan-91)
  26. ***
  27. ***********************************************************************/
  28. #include <exec/types.h>
  29. #include <exec/io.h>
  30. #include <devices/cdtv.h>
  31. #include <proto/all.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34.  
  35. #define FANCY
  36.  
  37. extern struct IOStdReq *CreateStdIO();
  38. extern struct MsgPort  *CreatePort();
  39. struct IOStdReq *IOReq1 = NULL;
  40. struct IOStdReq *IOReq2 = NULL;
  41. struct MsgPort  *IOPort = NULL;
  42.  
  43. int    Track;
  44. struct CDTOC   Toc[100];
  45. char   Buffer[10*2048];
  46.  
  47. char   AssertFail[] = "Assertion failed (MUST)";
  48.  
  49. #define MUST(expr)  if(!(expr)) Quit(AssertFail);
  50.  
  51. VOID Init(VOID);
  52. VOID Quit(char *);
  53. VOID DoIOR(struct IOStdReq *,int,long,long,APTR);
  54. VOID SendIOR(struct IOStdReq *,int,long,long,APTR);
  55. CDPOS AddMSF(CDPOS, CDPOS);
  56. VOID ReadTOC(VOID);
  57. VOID Play(VOID);
  58. VOID PlayBurst(VOID);
  59. VOID PlayBounce(VOID);
  60. VOID PlayFastFwd(VOID);
  61. VOID PlayAltFastFwd(VOID);
  62. VOID PlayShow(VOID);
  63. VOID PlayAbort(VOID);
  64. VOID PlayFade(VOID);
  65. VOID PlayMixedMode(VOID);
  66.  
  67.  
  68. /***********************************************************************
  69. ***
  70. ***  Main
  71. ***
  72. ***********************************************************************/
  73. VOID main(int argc,char *argv[])
  74. {
  75.  
  76.   printf("CDTV Audio Example (3-Jan-91)\n");
  77.  
  78.   Init();
  79.  
  80.   ReadTOC();    /* Read CD table of contents */
  81.  
  82.   DoIOR(IOReq1,CD_MUTE,0x7fff,1,0);    /* Set to full volume */
  83.  
  84.   Play();
  85.   PlayBurst();
  86.   PlayBounce();
  87.   PlayFastFwd();
  88.   PlayAltFastFwd();
  89.   PlayShow();
  90.   PlayAbort();
  91.   PlayFade();
  92.   PlayMixedMode();
  93.  
  94.   Quit(0);
  95.  
  96. }
  97.  
  98. /***********************************************************************
  99. ***
  100. ***  Init -- initialize program and structures
  101. ***
  102. ***********************************************************************/
  103. VOID Init(VOID)
  104. {
  105.  
  106.   MUST(IOPort = CreatePort(0,0));
  107.   MUST(IOReq1 = CreateStdIO(IOPort));
  108.   MUST(IOReq2 = CreateStdIO(IOPort));
  109.  
  110.   if(OpenDevice("cdtv.device",0,IOReq1,0))
  111.     Quit("CDTV Device will not open");
  112.  
  113.   *IOReq2 = *IOReq1;    /* Note: structure copy */
  114.  
  115. }
  116.  
  117. /***********************************************************************
  118. ***
  119. ***  Quit -- exit program and clean-up.  Return an error in needed.
  120. ***
  121. ***********************************************************************/
  122. VOID Quit(char *s)    /* error message */
  123. {
  124.  
  125.   if(IOReq1->io_Device) CloseDevice(IOReq1);
  126.   if(IOReq1) DeleteStdIO(IOReq1);
  127.   if(IOReq2) DeleteStdIO(IOReq2);
  128.   if(IOPort) DeletePort(IOPort);
  129.   if(s)
  130.     {
  131.     printf("\nERROR: %s\n",s);
  132.     exit(40);
  133.     }
  134.   else exit(0);
  135.  
  136. }
  137.  
  138. /***********************************************************************
  139. ***
  140. ***  DoIOR -- execute a device command
  141. ***
  142. ***********************************************************************/
  143. VOID DoIOR(struct IOStdReq *req,int cmd,long off,long len,APTR data)
  144. {
  145.  
  146.   req->io_Command = cmd;
  147.   req->io_Offset = off;
  148.   req->io_Length = len;
  149.   req->io_Data  = data;
  150.   if(DoIO(req)) Quit("DoIO command error");
  151.  
  152. }
  153.  
  154. /***********************************************************************
  155. ***
  156. ***  SendIOR -- asynchronously execute a device command
  157. ***
  158. ***********************************************************************/
  159. VOID SendIOR(struct IOStdReq *req,int cmd,long off,long len,APTR data)
  160. {
  161.  
  162.   req->io_Command = cmd;
  163.   req->io_Offset = off;
  164.   req->io_Length = len;
  165.   req->io_Data = data;
  166.   SendIO(req);
  167.  
  168. }
  169.  
  170. /***********************************************************************
  171. ***
  172. ***  AddMSF -- add two Min:Sec:Frm values
  173. ***
  174. ***********************************************************************/
  175. CDPOS AddMSF(CDPOS arg1,CDPOS arg2)
  176. {
  177.  
  178.   arg1.MSF.Frame += arg2.MSF.Frame;
  179.   if(arg1.MSF.Frame >= 75)
  180.     {
  181.     arg1.MSF.Frame  -= 75;
  182.     arg1.MSF.Second++;
  183.     }
  184.  
  185.   arg1.MSF.Second += arg2.MSF.Second;
  186.   if(arg1.MSF.Second >= 60)
  187.     {
  188.     arg1.MSF.Second -= 60;
  189.     arg1.MSF.Minute++;
  190.     }
  191.  
  192.   arg1.MSF.Minute += arg2.MSF.Minute;
  193.  
  194.   return(arg1);
  195.  
  196. }
  197.  
  198. /***********************************************************************
  199. ***
  200. ***  ReadTOC -- read entire CD table of contents
  201. ***
  202. ***********************************************************************/
  203. VOID ReadTOC(VOID)
  204. {
  205.  
  206.   char s[20];
  207.   SHORT i;
  208.  
  209.   DoIOR(IOReq1,CD_TOCMSF,0,100,&Toc[0]);
  210.  
  211.   printf("CD is %02d:%02d:%02d long ",Toc[0].Position.MSF.Minute,
  212.                       Toc[0].Position.MSF.Second,
  213.                       Toc[0].Position.MSF.Frame);
  214.   printf("with tracks %d to %d\n",Toc[0].Track,Toc[0].LastTrack);
  215.  
  216.   DoIOR(IOReq1,CD_ISROM,0,0,0);
  217.   printf("It %s CD-ROM data\n",    IOReq1->io_Actual ? "has" : "does not have");
  218.  
  219. /*--------------------------------------------------------------------*/
  220. #ifdef FANCY
  221.   for(i = 0;i<=Toc[0].LastTrack;i++)
  222.     {
  223.     printf("Track[%02d]  Type[%02x]  LSN[%ld]  %02d:%02d:%02d\n",
  224.         i,Toc[i].AddrCtrl,Toc[i].Position.LSN,
  225.                   Toc[i].Position.MSF.Minute,
  226.                   Toc[i].Position.MSF.Second,
  227.                   Toc[i].Position.MSF.Frame);
  228.     }
  229. #endif
  230. /*--------------------------------------------------------------------*/
  231.  
  232.   if(IOReq1->io_Actual && Toc[0].LastTrack <= 1)
  233.     Quit("No audio tracks\n");
  234.  
  235.   if(Toc[0].LastTrack <= 2)
  236.     Quit("Not enough tracks\n");
  237.  
  238. /*--------------------------------------------------------------------*/
  239. #ifdef FANCY
  240.   printf("Input Track number you want to play ->");
  241.   gets(s);
  242.   printf("\n");
  243.   Track = atoi(s);
  244. #else
  245.   Track = ((Toc[0].LastTrack >= 6) ? 7 : 2);
  246. #endif
  247. /*--------------------------------------------------------------------*/
  248.  
  249. }
  250.  
  251. /***********************************************************************
  252. ***
  253. ***  Example Functions
  254. ***
  255. ***********************************************************************/
  256. VOID Play(VOID)
  257. {
  258.  
  259.   CDPOS tm;
  260.  
  261.   printf("Play 20 seconds of track %d...\n",Track);
  262.  
  263.   tm.Raw = TOMSF(0,20,0);
  264.  
  265.   DoIOR(IOReq1,CD_PLAYMSF,Toc[Track].Position.Raw,
  266.         AddMSF(Toc[Track].Position,tm).Raw,0);
  267.  
  268. }
  269.  
  270. VOID PlayBurst(VOID)
  271. {
  272.  
  273.   int i;
  274.   CDPOS start, stop;
  275.   CDPOS tm;
  276.  
  277.   printf("Play 20 short sequential bursts...\n");
  278.   start.Raw = Toc[Track].Position.Raw;
  279.  
  280.   tm.Raw = TOMSF(0,0,19);
  281.  
  282.   for(i = 0; i < 40; i++)
  283.     {
  284.     stop = AddMSF(start,tm);
  285.     DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  286.     start.Raw = stop.Raw;
  287.   }
  288.  
  289. }
  290.  
  291. VOID PlayBounce(VOID)
  292. {
  293.  
  294.   int i;
  295.   CDPOS start, stop;
  296.   CDPOS tm;
  297.  
  298.   printf("Bounce the 'needle' on the 'record'...\n");
  299.  
  300.   tm.Raw = TOMSF(0,6,0);
  301.   start = AddMSF(Toc[Track].Position,tm);
  302.   tm.Raw = TOMSF(0,0,6);
  303.   stop = AddMSF(start,tm);
  304.  
  305.   for(i = 0; i < 30; i++)
  306.     {
  307.     DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  308.     }
  309.  
  310. }
  311.  
  312. VOID PlayFastFwd(VOID)
  313. {
  314.  
  315.   int i;
  316.   CDPOS start, stop;
  317.   CDPOS tm;
  318.  
  319.   printf("Play fast forward...\n");
  320.   start.Raw = Toc[Track].Position.Raw;
  321.  
  322.   for(i = 0; i < 40; i++)
  323.     {
  324.     tm.Raw = TOMSF(0,0,19);
  325.     stop = AddMSF(start,tm);
  326.     DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  327.     tm.Raw = TOMSF(0,1,0);
  328.     start = AddMSF(start,tm);
  329.     }
  330.  
  331. }
  332.  
  333. VOID PlayAltFastFwd(VOID)    /* Note: requires 2 I/O requests! */
  334. {
  335.  
  336.   int i;
  337.   CDPOS start, stop;
  338.   CDPOS tm;
  339.  
  340.   printf("Play alternate fast forward...\n");
  341.   start.Raw = Toc[Track].Position.Raw;
  342.  
  343.   tm.Raw = TOMSF(0,40,0);
  344.  
  345.   stop = AddMSF(start,tm);
  346.   SendIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  347.  
  348.   tm.Raw = TOMSF(0,0,30);
  349.  
  350.   for(i = 0; i < 30; i++)
  351.     {
  352.     Delay(50/5);    /* Note: Delay() not too accurate */
  353.     start = AddMSF(start,tm);
  354.     DoIOR(IOReq2,CD_POKEPLAYMSF,start.Raw,stop.Raw,0);
  355.     }
  356.  
  357.   AbortIO(IOReq1);
  358.   WaitIO(IOReq1);
  359.  
  360. }
  361.  
  362. VOID PlayShow(VOID)
  363. {
  364.  
  365.   CDPOS start, stop;
  366.   struct CDSubQ subq;
  367.   CDPOS tm;
  368.  
  369.   printf("Play and show track time...\n");
  370.   start.Raw = Toc[Track].Position.Raw;
  371.  
  372.   tm.Raw = TOMSF(0,20,0);
  373.  
  374.   stop = AddMSF(start,tm);
  375.   SendIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  376.  
  377.   while(!CheckIO(IOReq1))
  378.     {
  379.     DoIOR(IOReq2,CD_SUBQMSF,0,0,&subq);
  380.     if(subq.Status == SQSTAT_PLAYING)
  381.       {
  382.       printf("%02d:%02d:%02d\n",subq.TrackPosition.MSF.Minute,
  383.                 subq.TrackPosition.MSF.Second,
  384.                 subq.TrackPosition.MSF.Frame);
  385.       }
  386.     }
  387.  
  388.   WaitIO(IOReq1);
  389.  
  390. }
  391.  
  392. VOID PlayAbort(VOID)
  393. {
  394.  
  395.   CDPOS tm;
  396.  
  397.   printf("Play 20 sec, abort after 10...\n");
  398.  
  399.   tm.Raw = TOMSF(0,20,0);
  400.  
  401.   SendIOR(IOReq1,CD_PLAYMSF,Toc[Track].Position.Raw,
  402.         AddMSF(Toc[Track].Position,tm).Raw,0);
  403.  
  404.   Delay(50*10);
  405.   AbortIO(IOReq1);
  406.   WaitIO(IOReq1);
  407.   DoIOR(IOReq1,CD_STOPPLAY,0,0,0); /* only if you want to */
  408.  
  409. }
  410.  
  411. VOID PlayFade(VOID)
  412. {
  413.  
  414.   CDPOS start, stop;
  415.   int i;
  416.   CDPOS tm;
  417.  
  418.   printf("Play while fading up and down...\n");
  419.  
  420.   tm.Raw = TOMSF(1,0,0);
  421.  
  422.   start = AddMSF(Toc[Track].Position,tm);
  423.   stop = AddMSF(start,tm);
  424.   SendIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  425.  
  426.   DoIOR(IOReq2,CD_FADE,0x7fff,0,0);       /* full volume */
  427.  
  428.   for(i = 0; i < 5; i++)
  429.     {
  430.     DoIOR(IOReq2,CD_FADE,0,150,0);
  431.     Delay(100);
  432.     DoIOR(IOReq2,CD_FADE,0x7fff,150,0);
  433.     Delay(100);
  434.   }
  435.  
  436.   for(i = 0; i < 10; i++)
  437.     {
  438.     DoIOR(IOReq2,CD_FADE,0,15,0);
  439.     Delay(50/4);
  440.     DoIOR(IOReq2,CD_FADE,0x7fff,15,0);
  441.     Delay(50/4);
  442.     }
  443.  
  444.   DoIOR(IOReq2,CD_FADE,0,750,0);
  445.   Delay(50*10);
  446.  
  447.   AbortIO(IOReq1);
  448.   WaitIO(IOReq1);
  449.  
  450.   DoIOR(IOReq1,CD_STOPPLAY,0,0,0);    /* stop the play for good */
  451.   DoIOR(IOReq2,CD_FADE,0x7fff,0,0);    /* return to full vol      */
  452.  
  453. }
  454.  
  455. VOID PlayMixedMode(VOID)
  456. {
  457.  
  458.   int i;
  459.   CDPOS start, stop;
  460.   CDPOS tm;
  461.  
  462.   printf("Read and Play a mixed mode disk...\n");
  463.  
  464.   DoIOR(IOReq1,CD_ISROM,0,0,0);
  465.   if(!IOReq1->io_Actual)
  466.     {
  467.     printf("Not a mixed mode disk!\n");
  468.     return;
  469.     }
  470.  
  471.   start.Raw = Toc[Track].Position.Raw;
  472.  
  473.   tm.Raw = TOMSF(0,1,0);
  474.  
  475.   for(i = 0; i < 40; i++)
  476.     {
  477.     printf("Reading...\n");
  478.     DoIOR(IOReq1,CD_READ,i*10*2048,6*2048,Buffer);
  479.     if(i==0) printf("Whoops... will fix that glitch\n");
  480.     stop = AddMSF(start,tm);
  481.     printf("Playing...\n");
  482.     DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  483.     start.Raw = stop.Raw;
  484.     }
  485.  
  486. }
  487.